package cn.edu.tjut.control;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.edu.tjut.bean.StudentBean;
import cn.edu.tjut.myUtils.MD5Util;
import cn.edu.tjut.myUtils.MyUtils;
import cn.edu.tjut.seriver.StuService;

@Controller
public class StuInfoControl {
	// *************学生信息管理**************************************//
	@Resource
	private StuService stuService;

	// 跳转添加学生信息界面
	@RequestMapping(value = "/insertStuInfo.do")
	public String insertStuInfo0() {
		return "stuInfoManager/insertStuInfo";
	}

	// 添加学生信息
	@RequestMapping(value = "/Studetails.do")
	public String insertStuInfo(StudentBean stuB, HttpSession session, Model model) {
		if (stuB.getStuIndex() == null) {
			// 添加学生信息
			String md5psw = MD5Util.MD5(stuB.getStuPsw());
			stuB.setStuPsw(md5psw);
			int i = stuService.insertStuInfo(stuB);
			if (i > 0) {
				List<StudentBean> lstStu = stuService.selectAllstus();
				if (lstStu != null) {
					session.setAttribute("lstStu", lstStu);
					return "stuInfoManager/stuInfoDetails";
				} else {
					session.setAttribute("Prompt", "暂时没有学生信息");
					return "stuInfoManager/stuInfoDetails";
				}
			} else {
				session.setAttribute("Prompt", "添加学生信息失败！");
				return "stuInfoManager/stuInfoDetails";
			}
		}
		// 修改学生信息
		// 修改信息时如果修改密码，则将新密码设置MD5加密，如果不修改则不用设置。MD5密码长度统一为32位
		if (stuB.getStuPsw().length() < 30) {
			String MD5psw = MD5Util.MD5(stuB.getStuPsw());
			stuB.setStuPsw(MD5psw);
		}
		int i = stuService.updateBystuIndex(stuB);
		if (i > 0) {
			List<StudentBean> lstStu = stuService.selectAllstus();
			if (lstStu != null) {
				session.setAttribute("lstStu", lstStu);
				return "stuInfoManager/stuInfoDetails";
			} else {
				session.setAttribute("Prompt", "暂时没有学生信息");
				return "stuInfoManager/stuInfoDetails";
			}
		} else {
			session.setAttribute("Prompt", "修改学生信息失败！");
			return "stuInfoManager/stuInfoDetails";
		}
	}

	// 修改学生信息
	@RequestMapping(value = "/updateStuInfo.do")
	public String updateStuInfo(StudentBean stuB, HttpServletRequest request, HttpServletResponse response) {
		String stuIndex = request.getParameter("ope");
		stuB = stuService.selectBystuIndex(stuIndex);
		request.setAttribute("stuB", stuB);
		String opt = request.getParameter("opt");
		if (opt.equals("1")) {
			// 学生修改自己的信息
			/********************************************/
			return "";
		}
		return "stuInfoManager/insertStuInfo";
	}

	// 删除学生信息
	@RequestMapping(value = "/deleteStuInfo.do")
	public String deleteStuInfo(StudentBean stuB, HttpServletRequest request, HttpServletResponse response,
			HttpSession session) {
		String stuIndex = request.getParameter("ope");
		stuB.setStuIndex(MyUtils.obj2Integer(stuIndex));
		stuService.deleteBystuIndex(stuB);
		List<StudentBean> lstStu = stuService.selectAllstus();
		if (lstStu != null) {
			// 删除一个信息后，重新查询所有老师信息并显示
			return selectAllStuInfo(session);
		} else {
			session.setAttribute("Prompt", "暂时没有学生信息");
			return "stuInfoManager/stuInfoDetails";
		}
	}

	// 查询所有学生信息
	@RequestMapping(value = "/selectStu.do")
	public String selectAllStuInfo(HttpSession session) {
		List<StudentBean> lstStu = stuService.selectAllstus();
		if (lstStu != null) {
			session.setAttribute("lstStu", lstStu);
			return "stuInfoManager/stuInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有学生信息");
			return "stuInfoManager/stuInfoDetails";
		}
	}

	// 动态查询学生信息
	@RequestMapping(value = "/selectStuDynamic.do")
	public String selectStuDynamic0(StudentBean stuB) {
		return "stuInfoManager/selectStuDynamic";
	}

	// 动态查询学生信息
	@RequestMapping(value = "/StuDynamicdetails.do")
	public String selectStuDynamic(StudentBean stuB, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) {
		List<StudentBean> lstStu = stuService.selectStuInfoDynamic(stuB);
		if (lstStu != null) {
			session.setAttribute("lstStu", lstStu);
			return "stuInfoManager/stuInfoDetails";
		} else {
			session.setAttribute("Prompt", "暂时没有学生信息");
			return "stuInfoManager/stuInfoDetails";
		}
	}
}
